#!/usr/bin/env sh
#
# LEMP WordPress Reproducible Environment
#
# Currently only for development purposes
#
# Wishlist: Include XDebug

if [ ! $# -eq 2 ]; then
   cat << EOF
usage: $0 <shared_path> <public_path>
       shared_path: exchange directory
       public_path: public facing location, must be within shared_path

   ex: $0 /home/user/dev public_html
EOF
   exit 1
fi

shared_path="$1"
public_path="$1/$2"
file_path=$(dirname "$0") # path to this file

# Nginx
grep -qF "root $public_path" $file_path/etc/nginx/nginx.conf ||\
    sed -i\
        -e "s|#public_path#|$public_path|"\
        -e "s/#worker_connections#/$(ulimit -n)/"\
        $file_path/etc/nginx/nginx.conf

# MariaDB
grep -qF "#mysqld_user#" $file_path/etc/my.cnf &&\
    sed -i -e "s|#mysqld_user#|$(whoami)|" $file_path/etc/my.cnf

# Run the environment
guix environment\
     --ad-hoc --container --network --no-cwd\
     php mariadb nginx nss-certs busybox\
     --share=$file_path/etc=/usr/etc\
     --share=$file_path/var/log=/var/log\
     --share=$file_path/var/lib/mysql/data=/var/lib/mysql/data\
     --share=$shared_path
